{extend name="layout"/}
{block name="css"}
<style>
    .custom-control {
        margin-left: 18px
    }

    .pagination {
        margin: 0;
        float: right
    }
</style>
{/block}

{block name="content"}
<div class="panel">
    <div class="panel-heading">
        <span class="panel-title">模型列表</span>
        <div class="panel-heading-controls">
            <a class="btn btn-xs btn-primary" href="{:url('add')}"><i class="fa fa-plus"></i> 新增模型</a>
        </div>
    </div>
    <div class="panel-body">
        <form action="{:url('delete')}" method="post" id="tabledeal">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <tr>
                        <th width="60">ID</th>
                        <th>模型名称</th>
                        <th>表名</th>
                        <th width="70">类型</th>
                        <th width="80">用途</th>
                        <th width="160">创建时间</th>
                        <th width="80">数据管理</th>
                        <th width="60">投稿</th>
                        <th width="60">状态</th>
                        <th width="120">操作</th>
                    </tr>
                    {volist name="mlist" id="vo"}
                    <tr id="{$vo.id}">
                        <td>{$vo.id}</td>
                        <td>{$vo.title}</td>
                        <td>{$vo.table}</td>
                        <td>{if $vo.type==1}独立表{elseif $vo.type==2}主附表{/if}</td>
                        <td>{if $vo.purpose=='column'}栏目内容{elseif $vo.purpose=='independence'}独立内容{/if}</td>
                        <td>{$vo.create_time}</td>
                        <td>
                            <a class="btn btn-xs btn-primary" href="{:url('importdata',['mid'=>$vo.id])}" data-toggle="tooltip" data-placement="top" data-original-title="导入模型数据"><i class="fa fa-plus-square"></i></a>
                            <a class="btn btn-xs btn-primary" href="{:url('exportdata',['mid'=>$vo.id])}" data-toggle="tooltip" data-placement="top" data-original-title="导出模型数据"><i class="fa fa-share"></i></a>
                        </td>
                        <td>
                            {if $vo.ifsub}
                            <button data-id="0" type="button" class="btn btn-sm btn-primary btn-rounded ifsub">允许</button>
                            {else}
                            <button data-id="1" type="button" class="btn btn-sm btn-default btn-rounded ifsub">禁止</button>
                            {/if}
                        </td>
                        <td>
                            {if $vo.status}
                            <button data-id="0" type="button" class="btn btn-sm btn-primary btn-rounded status">启用</button>
                            {else}
                            <button data-id="1" type="button" class="btn btn-sm btn-default btn-rounded status">冻结</button>
                            {/if}
                        </td>
                        <td>
                            <a href="{:url('edit',['id'=>$vo.id])}"><i class="fa fa-edit btn  btn-sm btn-primary" data-toggle="tooltip" data-placement="top" data-original-title="编辑"></i></a>
                            <a href="{:url('field/index',['mid'=>$vo.id])}"><i class="fa fa-server btn  btn-sm btn-info" data-toggle="tooltip" data-placement="top" data-original-title="字段管理"></i></a>
                            <i class="fa fa-times btn btn-sm btn-warning del" data-toggle="tooltip" data-placement="top" data-original-title="删除"></i>
                        </td>
                    </tr>
                    {/volist}
                    {if !empty($page)}
                    <tr>
                        <td colspan="9">
                            {$page|raw}
                        </td>
                    </tr>
                    {/if}
                </table>
            </div>
        </form>
    </div>
</div>
{/block}

{block name="javascript"}
<script>
    $(function () {
        $('[data-toggle="tooltip"]').tooltip();
        $('.del').click(function () {
            var nowdom = $(this).parent().parent();
            bootbox.confirm({
                message: '确认删除吗？删除后不可恢复',
                className: 'bootbox-sm',
                callback: function (result) {
                    if (result) {
                        var id = nowdom.attr('id');
                        $.ajax({
                            url: "{:url('model/delete')}",
                            type: "GET",
                            data: {
                                id: id
                            },
                            dataType: 'text',
                            success: function (data) {
                                if (data === 'true') {
                                    nowdom.remove();
                                    toastr['success']('模型删除成功~', '', {
                                        closeButton: true
                                    });
                                } else {
                                    toastr['warning'](data, '', {
                                        closeButton: true
                                    });
                                }
                            }
                        });
                    }
                }
            });
        });
        $('.status').click(function () {
            var nowdom = $(this);
            var id = nowdom.parent().parent().attr('id');
            var status = nowdom.attr('data-id');
            $.ajax({
                url: "{:url('setstate')}",
                type: "GET",
                data: {
                    id: id,
                    status: status
                },
                dataType: 'text',
                success: function (data) {
                    if (data === 'true') {
                        if (status == 1) {
                            nowdom.addClass('btn-primary');
                            nowdom.attr('data-id', '0');
                            nowdom.html('启用');
                        }
                        if (status == 0) {
                            nowdom.removeClass('btn-primary');
                            nowdom.attr('data-id', '1');
                            nowdom.html('冻结');
                        }
                    } else {
                        toastr['warning'](data, '', {
                            closeButton: true
                        });
                    }
                }
            });
        });
        $('.ifsub').click(function () {
            var nowdom = $(this);
            var id = nowdom.parent().parent().attr('id');
            var status = nowdom.attr('data-id');
            $.ajax({
                url: "{:url('setsub')}",
                type: "GET",
                data: {
                    id: id,
                    status: status
                },
                dataType: 'text',
                success: function (data) {
                    if (data === 'true') {
                        if (status == 1) {
                            nowdom.addClass('btn-primary');
                            nowdom.attr('data-id', '0');
                            nowdom.html('允许');
                        }
                        if (status == 0) {
                            nowdom.removeClass('btn-primary');
                            nowdom.attr('data-id', '1');
                            nowdom.html('禁止');
                        }
                    } else {
                        toastr['warning'](data, '', {
                            closeButton: true
                        });
                    }
                }
            });
        });
    });
</script>
{/block}